package com.songcha.library_business.helper.permission

import android.Manifest
import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.os.Build
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import com.songcha.library_common.helper.PermissionTipHelper
import com.songcha.library_common.helper.SharedPreferenceHelper
import com.tbruyelle.rxpermissions3.RxPermissions

class WriteCalendarPermissionHelper {
    companion object{
        private var mPermissions: RxPermissions?= null

        @JvmStatic
        fun <T> hasPermission(t: T,block:(Int)->Unit){
            var activity: Activity? = null
            if(t is Fragment){
                mPermissions = RxPermissions(t)
                activity = t.requireActivity()
            }else if(t is FragmentActivity){
                mPermissions = RxPermissions(t)
                activity = t
            }

            if(activity == null) return

            //TODO
            try{
                mPermissions?.shouldShowRequestPermissionRationale(activity,Manifest.permission.READ_CALENDAR,Manifest.permission.WRITE_CALENDAR)
                    ?.subscribe ({
                        val isReadGranted = mPermissions?.isGranted(Manifest.permission.READ_CALENDAR)?:false
                        val isWriteGranted = mPermissions?.isGranted(Manifest.permission.WRITE_CALENDAR)?:false
                        if(isReadGranted && isWriteGranted){
                            block(PermissionResult.PERMISSION_GRANTED)
                        }else{
                            if(it){
                                block(PermissionResult.PERMISSION_DENY)
                            }else{
                                if(SharedPreferenceHelper.getBoolean("permission","calendar")){
                                    block(PermissionResult.PERMISSION_FOREVER_DENY)
                                }else{
                                    block(PermissionResult.PERMISSION_ASK)
                                }
                            }
                        }

                    },{

                    })
            }catch (e:Exception){
                e.printStackTrace()
            }

        }

        @JvmStatic
        fun <T> request(t: T,tip:String,onHasPermission:(isGranted:Boolean)->Unit,onPermissionDeny:(()->Unit)){
            var context: Context? = null

            if(t is Fragment)
                context = t.requireContext()
            else if(t is FragmentActivity)
                context = t
            if(context == null){
                return
            }

            hasPermission(t) { pm ->
                when (pm) {
                    PermissionResult.PERMISSION_ASK -> {
                        var dialogTip: Dialog? = null
                        if (context != null) {
                            dialogTip = PermissionTipHelper.showPermissionTip(context, tip)
                        }

                        SharedPreferenceHelper.setBoolean("permission","calendar",true)

                        mPermissions?.request(Manifest.permission.READ_CALENDAR,Manifest.permission.WRITE_CALENDAR)
                            ?.subscribe {
                                dialogTip?.dismiss()
                                if (it) {
                                    onHasPermission(true)
                                } else {
                                    onPermissionDeny()
                                }
                            }
                    }

                    PermissionResult.PERMISSION_GRANTED -> {
                        onHasPermission(false)
                    }

                    PermissionResult.PERMISSION_DENY,PermissionResult.PERMISSION_FOREVER_DENY -> {
                        onPermissionDeny()
                    }
                }
            }

        }

        @JvmStatic
        fun destroy(){
            mPermissions = null
        }

    }
}